package com.gisuni.sddc.platformManager.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONArray;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.UserInfo;
import com.gisuni.sddc.util.StringUtil;

@IocBean
public class UserDao {
	
	@Inject
	private Dao gisplatform;  
	
 
    public Boolean executeSql(String updateSql ){
    	
    	Sql sql = Sqls.create(updateSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				return true;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(Boolean.class );
 
    }
 
  
	/**
	 * 查询用户权限
	 * @param selectSql
	 * @return
	 */
	public String querySQL(String  selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				int rows = 0;
			     while (rs.next()){
			    	 JSONObject ret=new JSONObject();
			    	 rows++;
						// 遍历查询结果字段
						for (int i = 0; i < numberOfColumns; i++) {
							Object obj = rs.getString(i + 1);
							ret.put(rs.getMetaData().getColumnName(i+1).trim(), obj);
						}
			    	 result.put(ret);
			     }
			     JSONObject ret=new JSONObject();
			     result.put(ret);
				return result.toString();
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(String.class);
	}
	
	public String getControlRegion(String roleName){
		String result = "";
		if(!StringUtil.isNullOrEmpty(roleName)){
			
			
		}
		return result;
	}

	public List queryListSQL(String whereSel) {
		//TODO
		return gisplatform.query(UserInfo.class, null);
	}

	public int getMaxId(){
		Sql sql = Sqls.create("select max(USER_ID) USERID from SDDCGISPLATFORM.USER_INFO");
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				int userId = 0;;
			     while (rs.next()){
			    	 userId =  rs.getInt("USER_ID") ;
			     }
				return userId;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(int.class);
		
	}

	public UserInfo findUserInfoById(String selectSql) {
		Sql sql = Sqls.create(selectSql);
	    sql.setCallback(new SqlCallback() {
			public Object invoke(Connection conn, ResultSet rs, Sql sql)
					throws SQLException {
				JSONArray result=new JSONArray();
				int numberOfColumns = rs.getMetaData().getColumnCount();
				int rows = 0;
				UserInfo userInfo = new UserInfo();
			     while (rs.next()){
//			    	 JSONObject ret=new JSONObject();
//			    	 rows++;
//						// 遍历查询结果字段						
			    	 for (int i = 0; i < numberOfColumns; i++) {
						Object obj = rs.getString(i + 1);
//						System.out.println(rs.getMetaData().getColumnName(i+1).trim()+obj);
					}
//			    	 result.put(ret);
			    	 
			    	 userInfo.setUserId(rs.getString("USER_ID"));
			    	 userInfo.setUserName(rs.getString("USER_NAME"));
			    	 userInfo.setDisplayname(rs.getString("DISPLAYNAME"));
			    	 userInfo.setShortname(rs.getString("SHORTNAME"));
			    	 userInfo.setStatus(rs.getString("STATUS"));
			    	 userInfo.setEmail(rs.getString("EMAIL"));
			    	 userInfo.setNickname(rs.getString("NICKNAME"));
			    	 userInfo.setTelephone(rs.getString("TELEPHONE"));
			    	 userInfo.setSex(rs.getString("SEX"));
			    	 userInfo.setUserType(rs.getString("USER_TYPE"));
			    	 userInfo.setRoleIds(rs.getString("ROLEIDS"));
			    	 userInfo.setOrgCompanyId(rs.getString("ORGANIZATION_COMPANY_ID"));
			    	 userInfo.setOrgBussinessId(rs.getString("ORGANIZATION_BUSINESS_ID"));
			    	 userInfo.setOrgDevelopId(rs.getString("ORGANIZATION_DEVELOP_ID"));
			     }
			     JSONObject ret=new JSONObject();
				return userInfo;
			}
		});
	    gisplatform.execute(sql);
	    return  sql.getObject(UserInfo.class);
		 
	}




	public boolean delUserById(UserInfo user) {
		int res = gisplatform.delete(user);
		if(res>0){
			return true;
		}
		return false;
	}




	public boolean checkUserName(String userName) {
		if(!StringUtil.isNullOrEmpty(userName)){
			Cnd cnd = Cnd.where("USER_NAME","=",userName);
			List<UserInfo> res= gisplatform.query(UserInfo.class, cnd);
			if(res!=null && res.size()>0){
				return false;
			}
		}
		
		return true;
	}
	
	public boolean checkUserPwd(String userId,String pwd) {
			Cnd cnd = Cnd.where("user_Id", "=", userId).and("user_password","=",pwd);
			List<UserInfo> res= gisplatform.query(UserInfo.class, cnd);
			if(res!=null && res.size()>0){
				return true;
			}
		
		return false;
	}




 
 
	
}
